package com.ljd.service.impl;

import com.ljd.client.EduClient;
import com.ljd.client.UcenterClient;
import com.ljd.entity.Order;
import com.ljd.mapper.OrderMapper;
import com.ljd.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljd.util.JwtUtils;
import com.ljd.util.vo.CourseWebByOrderVo;
import com.ljd.util.vo.UcenterMemberVo;
import com.ljd.utils.OrderNoUtil;
import com.why.handler.GuliException;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.Odd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author ljd
 * @since 2022-01-05
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    EduClient eduClient;
    @Autowired
    UcenterClient ucenterClient;

    @Override
    public String createOrder(String courseId, HttpServletRequest request) {
        // 获取用户信息
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)){
            throw new GuliException(20001,"用户未登录");
        }
        UcenterMemberVo memberVo = ucenterClient.getUcenterMember(memberId);
        //获取课程信息
        CourseWebByOrderVo courseInfo = eduClient.getCourseInfo(courseId);

        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfo.getTitle());
        order.setCourseCover(courseInfo.getCover());
        order.setTeacherName(courseInfo.getTeacherName());
        order.setMemberId(memberVo.getId());
        order.setNickname(memberVo.getNickname());
        order.setMobile(memberVo.getMobile());
        order.setTotalFee(courseInfo.getPrice());
        order.setPayType(1);
        order.setStatus(0);
        baseMapper.insert(order);
        return order.getOrderNo();
    }
}
